<script setup lang="ts">
import { ref } from 'vue'
import * as video from '@/apis/video'
import * as admin from '@/apis/admin'

const version_ = ref('')
const versionUrl_ = ref('')
const loading = ref(true)

function calRes(e: any) {
  if (e.code != 200) {
    ElMessage(e.msg)
    return false
  }
  return true
}

video
  .configGet('version')
  .then(res => {
    if (calRes(res.data)) {
      version_.value = res.data.data
    }
    return video.configGet('versionUrl')
  })
  .then(res => {
    loading.value = false
    if (calRes(res.data)) {
      versionUrl_.value = res.data.data
    }
  })

function onSubmit() {
  admin
    .configSet('version', version_.value)
    .then(res => {
      if (calRes(res.data)) return admin.configSet('versionUrl', versionUrl_.value)
    })
    .then(res => {
      if (res) {
        loading.value = false
        ElMessage('设置完毕')
      }
    })
}
</script>
<template>
  <div>
    <el-card class="box-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <h2>版本控制</h2>
        </div>
      </template>
      <el-form label-width="120px">
        <el-form-item label="版本号">
          <el-input v-model="version_" />
        </el-form-item>
        <el-form-item label="下载地址">
          <el-input v-model="versionUrl_" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认设置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped></style>
